package groovyhelper;

import groovyhelper.GroovyBaseOverrideAction;
import groovyhelper.antlr.GroovyLexer;
import groovyhelper.antlr.GroovyRecognizer;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;

import java.io.StringReader;

import antlr.RecognitionException;
import antlr.TokenStreamException;
import antlr.collections.AST;
import org.apache.oro.text.regex.PatternMatcherInput;

public class GroovyTestAction extends GroovyBaseOverrideAction {
    private String spaces = "                                                                     ";

    void groovyActionPerformed(AnActionEvent anActionEvent, Editor editor, Project project, PsiFile psiFile) {
        String s = editor.getDocument().getText();
        GroovyLexer lexer = new GroovyLexer(new StringReader(s));
        GroovyRecognizer rec = new GroovyRecognizer(lexer);
        try {
            rec.compilationUnit();
            printTree(rec.getAST(), 0);
        } catch (RecognitionException e) {
            throw new RuntimeException(e);
        } catch (TokenStreamException e) {
            throw new RuntimeException(e);
        }
    }

    private void printTree(AST child, int ofs) {
        while (child != null) {
            System.out.println(spaces.substring(0, ofs) + "child = " + child + " : " + child.getType() + ":" + child.getLine() + ":" + child.getColumn());
            AST child1 = child.getFirstChild();
            if (child1 != null) {
                printTree(child1, ofs + 1);
            }
            child = child.getNextSibling();

        }
    }
}
